package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.FileMapper;
import com.winshare.saleAssistant.domain.dto.req.*;
import com.winshare.saleAssistant.domain.entity.FileDO;
import com.winshare.saleAssistant.domain.query.FileQuery;
import com.winshare.saleAssistant.service.FileManageService;
import com.winshare.saleAssistant.util.Page;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class fileManageServiceImpl implements FileManageService {

    private  FileMapper fileMapper;

    public fileManageServiceImpl(FileMapper fileMapper) {
        this.fileMapper = fileMapper;
    }

    @Override
    public void save(FileManageDTO fileManageDTO) {
        FileDO fileDO = new FileDO();
        fileDO.setFileTitle(fileManageDTO.getFileTitle());
        fileDO.setFileDesc(fileManageDTO.getFileDesc());
        fileDO.setImageUrl(fileManageDTO.getImageUrl());
        fileDO.setFileUrl(fileManageDTO.getFileUrl());
        fileDO.setFileName(fileManageDTO.getFileName());
        fileDO.setStatus(fileManageDTO.getStatus());
        fileMapper.saveFile(fileDO);
    }

    @Override
    public void update(FileManageDTO fileManageDTO) {
        FileDO fileDO = new FileDO();
        fileDO.setId(fileManageDTO.getId());
        fileDO.setFileTitle(fileManageDTO.getFileTitle());
        fileDO.setFileDesc(fileManageDTO.getFileDesc());
        fileDO.setImageUrl(fileManageDTO.getImageUrl());
        fileDO.setFileUrl(fileManageDTO.getFileUrl());
        fileDO.setFileName(fileManageDTO.getFileName());
        fileDO.setStatus(fileManageDTO.getStatus());
        fileMapper.updateFile(fileDO);
    }

    @Override
    public void delete(BaseIdDTO baseIdDTO) {
        FileDO fileDO = new FileDO();
        fileDO.setId(baseIdDTO.getId());
        fileMapper.deleteFile(fileDO);
    }

    @Override
    public Page<FileDO> getFileList(fileQueryDTO fileQueryDTO) {
        FileQuery fileQuery = new FileQuery();
        fileQuery.setFileTitle(fileQueryDTO.getFileTitle());
        fileQuery.setStatus(fileQueryDTO.getStatus());
        fileQuery.setOffsetIndex(fileQueryDTO.getOffsetIndex());
        fileQuery.setPageSize(fileQueryDTO.getPageSize());
        Integer pageIndex = fileQueryDTO.getPageIndex();
        Integer pageSize = fileQueryDTO.getPageSize();
        int totalCount = fileMapper.countFileListByConditions(fileQuery);
        if (0 == totalCount) {
            return new Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        }
        List<FileDO> list = fileMapper.findFileListByConditions(fileQuery);
        return new Page<>(pageIndex, pageSize, totalCount, list);
    }

    @Override
    public FileDO getFileDetail(BaseIdDTO baseIdDTO) {
        FileDO fileDO = new FileDO();
        fileDO.setId(baseIdDTO.getId());
        return fileMapper.getFile(fileDO);
    }
}
